﻿using System;
using System.Collections.Generic;
using System.Text;
using Lswweb.Model;
using Lswweb.Domain;
using Lswweb.Common;

namespace Lswweb.Controls
{
    public class ProductList : SkinnedGridView<ProductInfo>
    {
        protected override IList<ProductInfo> GridViewDataSourceHandler(int pageIndex, int pageSize, out int totalRecord)
        {
            int currentCategoryId = CategoryId == -1 ? BaseNodeId : CategoryId;

            return Products.GetProducts(
                SearchKey, 
                SearchType, 
                currentCategoryId, 
                IsWholeSale,
                Status, 
                AuditingStatusType.Approved, 
                IsHot, 
                IsTop, 
                new Pair<DateTime?>(MinDateTime,MaxDateTime), 
                PriceRange, 
                OrderBy,
                pageIndex, 
                pageSize, 
                out totalRecord);
        }

        private string searchKey = null;
        /// <summary>
        /// 默认值从Url中读取参数SearchKey
        /// </summary>
        public string SearchKey
        {
            get
            {
                if (searchKey == null)
                    return SiteContext.Current.SearchKey;
                else
                    return searchKey;
            }
            set
            {
                searchKey = value;
            }
        }

        public Boolean? IsWholeSale { get; set; }

        private SearchKeyType searchType = (SearchKeyType)(-1);
        /// <summary>
        /// 搜索类型，默认值为Url参数SearchType，如无Url参数SearchType则为NewsSearchKeyType.LoginName
        /// </summary>
        public SearchKeyType SearchType
        {
            get
            {
                if (Enum.IsDefined(typeof(SearchKeyType), searchType))
                    return searchType;
                else if (Enum.IsDefined(typeof(SearchKeyType), SiteContext.Current.SearchType))
                    return (SearchKeyType)SiteContext.Current.SearchType;
                else
                    return SearchKeyType.Title;
            }
            set
            {
                searchType = value;
            }
        }

        private StatusType? status = null;
        public StatusType? Status
        {
            get
            {
                if (this.status == null && Enum.IsDefined(typeof(StatusType), SiteContext.Current.Status))
                    this.status=(StatusType)SiteContext.Current.Status;

                return this.status;
            }
            set
            {
                status = value;
            }
        }

        private bool? iscommend = null;
        public bool? IsHot
        {
            get { return iscommend; }
            set { iscommend = value; }
        }

        private bool? istop = null;
        public bool? IsTop
        {
            get { return istop; }
            set { istop = value; }
        }

        private int categoryId = -1;
        public int CategoryId
        {
            get
            {
                if (categoryId == -1)
                    return SiteContext.Current.CategoryId;
                else
                    return categoryId;
            }
            set
            {
                categoryId = value;
            }
        }

        private int baseNodeId = -1;
        public int BaseNodeId
        {
            get { return baseNodeId == -1 ? SiteContext.Current.BaseNodeId : baseNodeId; }
            set { baseNodeId = value; }
        }

        private DateTime? maxDateTime = SiteContext.Current.MaxDateTime;
        public DateTime? MaxDateTime
        {
            get
            {
                if (this.maxDateTime == DateTime.MaxValue || this.maxDateTime==DateTime.MinValue)
                    return null;

                return (DateTime)maxDateTime;
            }
            set
            {
                maxDateTime = value;
            }
        }

        private DateTime? minDateTime = SiteContext.Current.MinDateTime;
        public DateTime? MinDateTime
        {
            get
            {
                if (this.minDateTime == DateTime.MinValue)
                    return null;

                return this.minDateTime;
            }
            set
            {
                minDateTime = value;
            }
        }

        /// <summary>
        /// 配置为Resource.xml中的name值
        /// </summary>
        public string IconStr { get; set; }

        private bool enableDelButton = true;
        public bool EnableDelButton
        {
            get
            {
                if (SiteContext.Current.EnableDelButton != enableDelButton)
                    enableDelButton = SiteContext.Current.EnableDelButton;

                return enableDelButton;
            }
            set { enableDelButton = value; }
        }

        private bool enableTopButton = SiteContext.Current.EnableTopButton;
        public bool EnableTopBtn
        {
            get { return enableTopButton; }
            set { enableTopButton = value; }
        }

        private bool enableHotButton = SiteContext.Current.EnableHotButton;
        public bool EnableHotBtn
        {
            get{return enableHotButton;}
            set { enableHotButton = value; }
        }

        //2010-1129

        public Pair<Decimal?> PriceRange { get; set; }

        public string OrderBy { get; set; }

        protected override string DefaultSkinFilename
        {
            get { return "Products/ProductList.ascx"; }
        }
    }
}
